/** 这个给 background.js 使用 */
const baseApi = async params => {
  try {
    let { url, data, method, headers } = params;
    if (!method) {
      method = "POST";
    }
    console.log(url);
    let { token, bid, baseUrl } = await chrome.storage.local.get([
      "bid",
      "token",
      "baseUrl"
    ]);
    console.log("token", token);
    console.log("bin", bid);
    console.log("baseUrl", baseUrl);
    if (baseUrl) {
      // baseUrl = JSON.parse(baseUrl);
      // baseUrl = atob(baseUrl.baseUrl);
      // console.log("baseUrl", baseUrl);
      url = baseUrl + url;
    }
    console.log(url);

    // Default options are marked with *
    const response = await fetch(url, {
      method: method, // *GET, POST, PUT, DELETE, etc.
      mode: "cors", // no-cors, *cors, same-origin
      cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
      credentials: "same-origin", // include, *same-origin, omit
      headers: {
        "Content-Type": "application/json",
        // 'Content-Type': 'application/x-www-form-urlencoded',
        Bid: bid,
        Platform: "CHROME_PLUGIN",
        Authorization: token,
        ...headers
      },
      redirect: "follow", // manual, *follow, error
      referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
      body: JSON.stringify(data) // body data type must match "Content-Type" header
    });
    if (response.status === 404) {
      return { code: -404, _success, _msg: "服务端接口不存在" };
    }
    const res = await response.json();
    let { _success, _data, _code, _msg } = res;

    if (_success) {
      return { data: _data, code: _code, _success, _msg, _data };
    } else {
      return { code: _code, _success, _msg };
    }
  } catch (e) {
    console.error("接口异常", e);
    throw { code: -1, _success: false, _msg: (e && e.message) || e };
  }
};

export const downloadThirdpartApi = async params => {
  // console.log("downloadThirdpartApi", params);
  let { url, data, method, headers } = params;
  if (!method) {
    method = "POST";
  } // Default options are marked with *
  const response = await fetch(url, {
    method: method, // *GET, POST, PUT, DELETE, etc.
    mode: "cors", // no-cors, *cors, same-origin
    cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
    // credentials: "same-origin", // include, *same-origin, omit
    redirect: "follow", // manual, *follow, error
    referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  });
  // console.log("downloadThirdpartApi", response);
  if (response.status === 404) {
    return { code: -404, _success: false, _msg: "服务端接口不存在" };
  }
  const blob = await response.blob();
  // console.log("blob", blob);
  return blob;
};

export default baseApi;
